﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using VCS.Entities;
using VCS.Data;

#endregion

namespace VCS.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="InvoiceProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class InvoiceProviderBaseCore : EntityProviderBase<VCS.Entities.Invoice, VCS.Entities.InvoiceKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, VCS.Entities.InvoiceKey key)
		{
			return Delete(transactionManager, key.InvoiceId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_invoiceId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _invoiceId)
		{
			return Delete(null, _invoiceId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_invoiceId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _invoiceId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		FK_Invoice_Company Description: 
		/// </summary>
		/// <param name="_companyCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByCompanyCode(System.String _companyCode)
		{
			int count = -1;
			return GetByCompanyCode(_companyCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		FK_Invoice_Company Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		/// <remarks></remarks>
		public TList<Invoice> GetByCompanyCode(TransactionManager transactionManager, System.String _companyCode)
		{
			int count = -1;
			return GetByCompanyCode(transactionManager, _companyCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		FK_Invoice_Company Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByCompanyCode(TransactionManager transactionManager, System.String _companyCode, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyCode(transactionManager, _companyCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		fkInvoiceCompany Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByCompanyCode(System.String _companyCode, int start, int pageLength)
		{
			int count =  -1;
			return GetByCompanyCode(null, _companyCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		fkInvoiceCompany Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByCompanyCode(System.String _companyCode, int start, int pageLength,out int count)
		{
			return GetByCompanyCode(null, _companyCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Company key.
		///		FK_Invoice_Company Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public abstract TList<Invoice> GetByCompanyCode(TransactionManager transactionManager, System.String _companyCode, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		FK_Invoice_Patient Description: 
		/// </summary>
		/// <param name="_patientCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPatientCode(System.String _patientCode)
		{
			int count = -1;
			return GetByPatientCode(_patientCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		FK_Invoice_Patient Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_patientCode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		/// <remarks></remarks>
		public TList<Invoice> GetByPatientCode(TransactionManager transactionManager, System.String _patientCode)
		{
			int count = -1;
			return GetByPatientCode(transactionManager, _patientCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		FK_Invoice_Patient Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_patientCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPatientCode(TransactionManager transactionManager, System.String _patientCode, int start, int pageLength)
		{
			int count = -1;
			return GetByPatientCode(transactionManager, _patientCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		fkInvoicePatient Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_patientCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPatientCode(System.String _patientCode, int start, int pageLength)
		{
			int count =  -1;
			return GetByPatientCode(null, _patientCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		fkInvoicePatient Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_patientCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPatientCode(System.String _patientCode, int start, int pageLength,out int count)
		{
			return GetByPatientCode(null, _patientCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_Patient key.
		///		FK_Invoice_Patient Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_patientCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public abstract TList<Invoice> GetByPatientCode(TransactionManager transactionManager, System.String _patientCode, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		FK_Invoice_PatientActivation Description: 
		/// </summary>
		/// <param name="_transactionId"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByTransactionId(System.String _transactionId)
		{
			int count = -1;
			return GetByTransactionId(_transactionId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		FK_Invoice_PatientActivation Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_transactionId"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		/// <remarks></remarks>
		public TList<Invoice> GetByTransactionId(TransactionManager transactionManager, System.String _transactionId)
		{
			int count = -1;
			return GetByTransactionId(transactionManager, _transactionId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		FK_Invoice_PatientActivation Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_transactionId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByTransactionId(TransactionManager transactionManager, System.String _transactionId, int start, int pageLength)
		{
			int count = -1;
			return GetByTransactionId(transactionManager, _transactionId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		fkInvoicePatientActivation Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_transactionId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByTransactionId(System.String _transactionId, int start, int pageLength)
		{
			int count =  -1;
			return GetByTransactionId(null, _transactionId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		fkInvoicePatientActivation Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_transactionId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByTransactionId(System.String _transactionId, int start, int pageLength,out int count)
		{
			return GetByTransactionId(null, _transactionId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PatientActivation key.
		///		FK_Invoice_PatientActivation Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_transactionId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public abstract TList<Invoice> GetByTransactionId(TransactionManager transactionManager, System.String _transactionId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		FK_Invoice_PaymentMode Description: 
		/// </summary>
		/// <param name="_paymentMode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPaymentMode(System.String _paymentMode)
		{
			int count = -1;
			return GetByPaymentMode(_paymentMode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		FK_Invoice_PaymentMode Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_paymentMode"></param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		/// <remarks></remarks>
		public TList<Invoice> GetByPaymentMode(TransactionManager transactionManager, System.String _paymentMode)
		{
			int count = -1;
			return GetByPaymentMode(transactionManager, _paymentMode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		FK_Invoice_PaymentMode Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_paymentMode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPaymentMode(TransactionManager transactionManager, System.String _paymentMode, int start, int pageLength)
		{
			int count = -1;
			return GetByPaymentMode(transactionManager, _paymentMode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		fkInvoicePaymentMode Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_paymentMode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPaymentMode(System.String _paymentMode, int start, int pageLength)
		{
			int count =  -1;
			return GetByPaymentMode(null, _paymentMode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		fkInvoicePaymentMode Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_paymentMode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public TList<Invoice> GetByPaymentMode(System.String _paymentMode, int start, int pageLength,out int count)
		{
			return GetByPaymentMode(null, _paymentMode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Invoice_PaymentMode key.
		///		FK_Invoice_PaymentMode Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_paymentMode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of VCS.Entities.Invoice objects.</returns>
		public abstract TList<Invoice> GetByPaymentMode(TransactionManager transactionManager, System.String _paymentMode, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override VCS.Entities.Invoice Get(TransactionManager transactionManager, VCS.Entities.InvoiceKey key, int start, int pageLength)
		{
			return GetByInvoiceId(transactionManager, key.InvoiceId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Invoice index.
		/// </summary>
		/// <param name="_invoiceId"></param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public VCS.Entities.Invoice GetByInvoiceId(System.String _invoiceId)
		{
			int count = -1;
			return GetByInvoiceId(null,_invoiceId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Invoice index.
		/// </summary>
		/// <param name="_invoiceId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public VCS.Entities.Invoice GetByInvoiceId(System.String _invoiceId, int start, int pageLength)
		{
			int count = -1;
			return GetByInvoiceId(null, _invoiceId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Invoice index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_invoiceId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public VCS.Entities.Invoice GetByInvoiceId(TransactionManager transactionManager, System.String _invoiceId)
		{
			int count = -1;
			return GetByInvoiceId(transactionManager, _invoiceId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Invoice index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_invoiceId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public VCS.Entities.Invoice GetByInvoiceId(TransactionManager transactionManager, System.String _invoiceId, int start, int pageLength)
		{
			int count = -1;
			return GetByInvoiceId(transactionManager, _invoiceId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Invoice index.
		/// </summary>
		/// <param name="_invoiceId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public VCS.Entities.Invoice GetByInvoiceId(System.String _invoiceId, int start, int pageLength, out int count)
		{
			return GetByInvoiceId(null, _invoiceId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Invoice index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_invoiceId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="VCS.Entities.Invoice"/> class.</returns>
		public abstract VCS.Entities.Invoice GetByInvoiceId(TransactionManager transactionManager, System.String _invoiceId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Invoice&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Invoice&gt;"/></returns>
		public static TList<Invoice> Fill(IDataReader reader, TList<Invoice> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				VCS.Entities.Invoice c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Invoice")
					.Append("|").Append((System.String)reader[((int)InvoiceColumn.InvoiceId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Invoice>(
					key.ToString(), // EntityTrackingKey
					"Invoice",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new VCS.Entities.Invoice();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.InvoiceId = (System.String)reader[((int)InvoiceColumn.InvoiceId - 1)];
					c.OriginalInvoiceId = c.InvoiceId;
					c.InvoiceDate = (System.DateTime)reader[((int)InvoiceColumn.InvoiceDate - 1)];
					c.PrescriptionId = (System.String)reader[((int)InvoiceColumn.PrescriptionId - 1)];
					c.TransactionId = (System.String)reader[((int)InvoiceColumn.TransactionId - 1)];
					c.FormNo = (System.String)reader[((int)InvoiceColumn.FormNo - 1)];
					c.Serial = (System.String)reader[((int)InvoiceColumn.Serial - 1)];
					c.LocationCode = (System.String)reader[((int)InvoiceColumn.LocationCode - 1)];
					c.LocationNumber = (System.String)reader[((int)InvoiceColumn.LocationNumber - 1)];
					c.PatientCode = (System.String)reader[((int)InvoiceColumn.PatientCode - 1)];
					c.FirstName = (System.String)reader[((int)InvoiceColumn.FirstName - 1)];
					c.LastName = (System.String)reader[((int)InvoiceColumn.LastName - 1)];
					c.Nationality = (System.String)reader[((int)InvoiceColumn.Nationality - 1)];
					c.DateOfBirth = (reader.IsDBNull(((int)InvoiceColumn.DateOfBirth - 1)))?null:(System.DateTime?)reader[((int)InvoiceColumn.DateOfBirth - 1)];
					c.Sex = (System.String)reader[((int)InvoiceColumn.Sex - 1)];
					c.CompanyCode = (System.String)reader[((int)InvoiceColumn.CompanyCode - 1)];
					c.CompanyName = (System.String)reader[((int)InvoiceColumn.CompanyName - 1)];
					c.Address = (reader.IsDBNull(((int)InvoiceColumn.Address - 1)))?null:(System.String)reader[((int)InvoiceColumn.Address - 1)];
					c.TaxCode = (reader.IsDBNull(((int)InvoiceColumn.TaxCode - 1)))?null:(System.String)reader[((int)InvoiceColumn.TaxCode - 1)];
					c.PaymentMode = (System.String)reader[((int)InvoiceColumn.PaymentMode - 1)];
					c.SubTotal = (System.Decimal)reader[((int)InvoiceColumn.SubTotal - 1)];
					c.Vat = (System.Decimal)reader[((int)InvoiceColumn.Vat - 1)];
					c.Total = (System.Decimal)reader[((int)InvoiceColumn.Total - 1)];
					c.ExchangeRate = (reader.IsDBNull(((int)InvoiceColumn.ExchangeRate - 1)))?null:(System.Int32?)reader[((int)InvoiceColumn.ExchangeRate - 1)];
					c.UsdAmount = (System.Decimal)reader[((int)InvoiceColumn.UsdAmount - 1)];
					c.IsVoid = (System.Boolean)reader[((int)InvoiceColumn.IsVoid - 1)];
					c.Remark = (reader.IsDBNull(((int)InvoiceColumn.Remark - 1)))?null:(System.String)reader[((int)InvoiceColumn.Remark - 1)];
					c.UpdateDate = (System.DateTime)reader[((int)InvoiceColumn.UpdateDate - 1)];
					c.CreateUser = (reader.IsDBNull(((int)InvoiceColumn.CreateUser - 1)))?null:(System.String)reader[((int)InvoiceColumn.CreateUser - 1)];
					c.CreateDate = (System.DateTime)reader[((int)InvoiceColumn.CreateDate - 1)];
					c.UpdateUser = (reader.IsDBNull(((int)InvoiceColumn.UpdateUser - 1)))?null:(System.String)reader[((int)InvoiceColumn.UpdateUser - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.Invoice"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.Invoice"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, VCS.Entities.Invoice entity)
		{
			if (!reader.Read()) return;
			
			entity.InvoiceId = (System.String)reader[((int)InvoiceColumn.InvoiceId - 1)];
			entity.OriginalInvoiceId = (System.String)reader["InvoiceID"];
			entity.InvoiceDate = (System.DateTime)reader[((int)InvoiceColumn.InvoiceDate - 1)];
			entity.PrescriptionId = (System.String)reader[((int)InvoiceColumn.PrescriptionId - 1)];
			entity.TransactionId = (System.String)reader[((int)InvoiceColumn.TransactionId - 1)];
			entity.FormNo = (System.String)reader[((int)InvoiceColumn.FormNo - 1)];
			entity.Serial = (System.String)reader[((int)InvoiceColumn.Serial - 1)];
			entity.LocationCode = (System.String)reader[((int)InvoiceColumn.LocationCode - 1)];
			entity.LocationNumber = (System.String)reader[((int)InvoiceColumn.LocationNumber - 1)];
			entity.PatientCode = (System.String)reader[((int)InvoiceColumn.PatientCode - 1)];
			entity.FirstName = (System.String)reader[((int)InvoiceColumn.FirstName - 1)];
			entity.LastName = (System.String)reader[((int)InvoiceColumn.LastName - 1)];
			entity.Nationality = (System.String)reader[((int)InvoiceColumn.Nationality - 1)];
			entity.DateOfBirth = (reader.IsDBNull(((int)InvoiceColumn.DateOfBirth - 1)))?null:(System.DateTime?)reader[((int)InvoiceColumn.DateOfBirth - 1)];
			entity.Sex = (System.String)reader[((int)InvoiceColumn.Sex - 1)];
			entity.CompanyCode = (System.String)reader[((int)InvoiceColumn.CompanyCode - 1)];
			entity.CompanyName = (System.String)reader[((int)InvoiceColumn.CompanyName - 1)];
			entity.Address = (reader.IsDBNull(((int)InvoiceColumn.Address - 1)))?null:(System.String)reader[((int)InvoiceColumn.Address - 1)];
			entity.TaxCode = (reader.IsDBNull(((int)InvoiceColumn.TaxCode - 1)))?null:(System.String)reader[((int)InvoiceColumn.TaxCode - 1)];
			entity.PaymentMode = (System.String)reader[((int)InvoiceColumn.PaymentMode - 1)];
			entity.SubTotal = (System.Decimal)reader[((int)InvoiceColumn.SubTotal - 1)];
			entity.Vat = (System.Decimal)reader[((int)InvoiceColumn.Vat - 1)];
			entity.Total = (System.Decimal)reader[((int)InvoiceColumn.Total - 1)];
			entity.ExchangeRate = (reader.IsDBNull(((int)InvoiceColumn.ExchangeRate - 1)))?null:(System.Int32?)reader[((int)InvoiceColumn.ExchangeRate - 1)];
			entity.UsdAmount = (System.Decimal)reader[((int)InvoiceColumn.UsdAmount - 1)];
			entity.IsVoid = (System.Boolean)reader[((int)InvoiceColumn.IsVoid - 1)];
			entity.Remark = (reader.IsDBNull(((int)InvoiceColumn.Remark - 1)))?null:(System.String)reader[((int)InvoiceColumn.Remark - 1)];
			entity.UpdateDate = (System.DateTime)reader[((int)InvoiceColumn.UpdateDate - 1)];
			entity.CreateUser = (reader.IsDBNull(((int)InvoiceColumn.CreateUser - 1)))?null:(System.String)reader[((int)InvoiceColumn.CreateUser - 1)];
			entity.CreateDate = (System.DateTime)reader[((int)InvoiceColumn.CreateDate - 1)];
			entity.UpdateUser = (reader.IsDBNull(((int)InvoiceColumn.UpdateUser - 1)))?null:(System.String)reader[((int)InvoiceColumn.UpdateUser - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="VCS.Entities.Invoice"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VCS.Entities.Invoice"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, VCS.Entities.Invoice entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.InvoiceId = (System.String)dataRow["InvoiceID"];
			entity.OriginalInvoiceId = (System.String)dataRow["InvoiceID"];
			entity.InvoiceDate = (System.DateTime)dataRow["InvoiceDate"];
			entity.PrescriptionId = (System.String)dataRow["PrescriptionID"];
			entity.TransactionId = (System.String)dataRow["TransactionID"];
			entity.FormNo = (System.String)dataRow["FormNo"];
			entity.Serial = (System.String)dataRow["Serial"];
			entity.LocationCode = (System.String)dataRow["LocationCode"];
			entity.LocationNumber = (System.String)dataRow["LocationNumber"];
			entity.PatientCode = (System.String)dataRow["PatientCode"];
			entity.FirstName = (System.String)dataRow["FirstName"];
			entity.LastName = (System.String)dataRow["LastName"];
			entity.Nationality = (System.String)dataRow["Nationality"];
			entity.DateOfBirth = Convert.IsDBNull(dataRow["DateOfBirth"]) ? null : (System.DateTime?)dataRow["DateOfBirth"];
			entity.Sex = (System.String)dataRow["Sex"];
			entity.CompanyCode = (System.String)dataRow["CompanyCode"];
			entity.CompanyName = (System.String)dataRow["CompanyName"];
			entity.Address = Convert.IsDBNull(dataRow["Address"]) ? null : (System.String)dataRow["Address"];
			entity.TaxCode = Convert.IsDBNull(dataRow["TaxCode"]) ? null : (System.String)dataRow["TaxCode"];
			entity.PaymentMode = (System.String)dataRow["PaymentMode"];
			entity.SubTotal = (System.Decimal)dataRow["SubTotal"];
			entity.Vat = (System.Decimal)dataRow["VAT"];
			entity.Total = (System.Decimal)dataRow["Total"];
			entity.ExchangeRate = Convert.IsDBNull(dataRow["ExchangeRate"]) ? null : (System.Int32?)dataRow["ExchangeRate"];
			entity.UsdAmount = (System.Decimal)dataRow["USDAmount"];
			entity.IsVoid = (System.Boolean)dataRow["IsVoid"];
			entity.Remark = Convert.IsDBNull(dataRow["Remark"]) ? null : (System.String)dataRow["Remark"];
			entity.UpdateDate = (System.DateTime)dataRow["UpdateDate"];
			entity.CreateUser = Convert.IsDBNull(dataRow["CreateUser"]) ? null : (System.String)dataRow["CreateUser"];
			entity.CreateDate = (System.DateTime)dataRow["CreateDate"];
			entity.UpdateUser = Convert.IsDBNull(dataRow["UpdateUser"]) ? null : (System.String)dataRow["UpdateUser"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="VCS.Entities.Invoice"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">VCS.Entities.Invoice Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, VCS.Entities.Invoice entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region CompanyCodeSource	
			if (CanDeepLoad(entity, "Company|CompanyCodeSource", deepLoadType, innerList) 
				&& entity.CompanyCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.CompanyCode;
				Company tmpEntity = EntityManager.LocateEntity<Company>(EntityLocator.ConstructKeyFromPkItems(typeof(Company), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.CompanyCodeSource = tmpEntity;
				else
					entity.CompanyCodeSource = DataRepository.CompanyProvider.GetByCompanyCode(transactionManager, entity.CompanyCode);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.CompanyProvider.DeepLoad(transactionManager, entity.CompanyCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion CompanyCodeSource

			#region PatientCodeSource	
			if (CanDeepLoad(entity, "Patient|PatientCodeSource", deepLoadType, innerList) 
				&& entity.PatientCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.PatientCode;
				Patient tmpEntity = EntityManager.LocateEntity<Patient>(EntityLocator.ConstructKeyFromPkItems(typeof(Patient), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.PatientCodeSource = tmpEntity;
				else
					entity.PatientCodeSource = DataRepository.PatientProvider.GetByPatientCode(transactionManager, entity.PatientCode);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatientCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.PatientCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PatientProvider.DeepLoad(transactionManager, entity.PatientCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion PatientCodeSource

			#region TransactionIdSource	
			if (CanDeepLoad(entity, "PatientActivation|TransactionIdSource", deepLoadType, innerList) 
				&& entity.TransactionIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.TransactionId;
				PatientActivation tmpEntity = EntityManager.LocateEntity<PatientActivation>(EntityLocator.ConstructKeyFromPkItems(typeof(PatientActivation), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.TransactionIdSource = tmpEntity;
				else
					entity.TransactionIdSource = DataRepository.PatientActivationProvider.GetByTransactionId(transactionManager, entity.TransactionId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'TransactionIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.TransactionIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PatientActivationProvider.DeepLoad(transactionManager, entity.TransactionIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion TransactionIdSource

			#region PaymentModeSource	
			if (CanDeepLoad(entity, "PaymentMode|PaymentModeSource", deepLoadType, innerList) 
				&& entity.PaymentModeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.PaymentMode;
				PaymentMode tmpEntity = EntityManager.LocateEntity<PaymentMode>(EntityLocator.ConstructKeyFromPkItems(typeof(PaymentMode), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.PaymentModeSource = tmpEntity;
				else
					entity.PaymentModeSource = DataRepository.PaymentModeProvider.GetByPaymentMode(transactionManager, entity.PaymentMode);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PaymentModeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.PaymentModeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PaymentModeProvider.DeepLoad(transactionManager, entity.PaymentModeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion PaymentModeSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByInvoiceId methods when available
			
			#region InvoiceDetailsCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<InvoiceDetails>|InvoiceDetailsCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'InvoiceDetailsCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.InvoiceDetailsCollection = DataRepository.InvoiceDetailsProvider.GetByInvoiceId(transactionManager, entity.InvoiceId);

				if (deep && entity.InvoiceDetailsCollection.Count > 0)
				{
					deepHandles.Add("InvoiceDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<InvoiceDetails>) DataRepository.InvoiceDetailsProvider.DeepLoad,
						new object[] { transactionManager, entity.InvoiceDetailsCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region PrescriptionDetailCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PrescriptionDetail>|PrescriptionDetailCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PrescriptionDetailCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PrescriptionDetailCollection = DataRepository.PrescriptionDetailProvider.GetByInvoiceId(transactionManager, entity.InvoiceId);

				if (deep && entity.PrescriptionDetailCollection.Count > 0)
				{
					deepHandles.Add("PrescriptionDetailCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PrescriptionDetail>) DataRepository.PrescriptionDetailProvider.DeepLoad,
						new object[] { transactionManager, entity.PrescriptionDetailCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the VCS.Entities.Invoice object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">VCS.Entities.Invoice instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">VCS.Entities.Invoice Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, VCS.Entities.Invoice entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region CompanyCodeSource
			if (CanDeepSave(entity, "Company|CompanyCodeSource", deepSaveType, innerList) 
				&& entity.CompanyCodeSource != null)
			{
				DataRepository.CompanyProvider.Save(transactionManager, entity.CompanyCodeSource);
				entity.CompanyCode = entity.CompanyCodeSource.CompanyCode;
			}
			#endregion 
			
			#region PatientCodeSource
			if (CanDeepSave(entity, "Patient|PatientCodeSource", deepSaveType, innerList) 
				&& entity.PatientCodeSource != null)
			{
				DataRepository.PatientProvider.Save(transactionManager, entity.PatientCodeSource);
				entity.PatientCode = entity.PatientCodeSource.PatientCode;
			}
			#endregion 
			
			#region TransactionIdSource
			if (CanDeepSave(entity, "PatientActivation|TransactionIdSource", deepSaveType, innerList) 
				&& entity.TransactionIdSource != null)
			{
				DataRepository.PatientActivationProvider.Save(transactionManager, entity.TransactionIdSource);
				entity.TransactionId = entity.TransactionIdSource.TransactionId;
			}
			#endregion 
			
			#region PaymentModeSource
			if (CanDeepSave(entity, "PaymentMode|PaymentModeSource", deepSaveType, innerList) 
				&& entity.PaymentModeSource != null)
			{
				DataRepository.PaymentModeProvider.Save(transactionManager, entity.PaymentModeSource);
				entity.PaymentMode = entity.PaymentModeSource.PaymentMode;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<InvoiceDetails>
				if (CanDeepSave(entity.InvoiceDetailsCollection, "List<InvoiceDetails>|InvoiceDetailsCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(InvoiceDetails child in entity.InvoiceDetailsCollection)
					{
						if(child.InvoiceIdSource != null)
						{
							child.InvoiceId = child.InvoiceIdSource.InvoiceId;
						}
						else
						{
							child.InvoiceId = entity.InvoiceId;
						}

					}

					if (entity.InvoiceDetailsCollection.Count > 0 || entity.InvoiceDetailsCollection.DeletedItems.Count > 0)
					{
						//DataRepository.InvoiceDetailsProvider.Save(transactionManager, entity.InvoiceDetailsCollection);
						
						deepHandles.Add("InvoiceDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< InvoiceDetails >) DataRepository.InvoiceDetailsProvider.DeepSave,
							new object[] { transactionManager, entity.InvoiceDetailsCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<PrescriptionDetail>
				if (CanDeepSave(entity.PrescriptionDetailCollection, "List<PrescriptionDetail>|PrescriptionDetailCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PrescriptionDetail child in entity.PrescriptionDetailCollection)
					{
						if(child.InvoiceIdSource != null)
						{
							child.InvoiceId = child.InvoiceIdSource.InvoiceId;
						}
						else
						{
							child.InvoiceId = entity.InvoiceId;
						}

					}

					if (entity.PrescriptionDetailCollection.Count > 0 || entity.PrescriptionDetailCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PrescriptionDetailProvider.Save(transactionManager, entity.PrescriptionDetailCollection);
						
						deepHandles.Add("PrescriptionDetailCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PrescriptionDetail >) DataRepository.PrescriptionDetailProvider.DeepSave,
							new object[] { transactionManager, entity.PrescriptionDetailCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region InvoiceChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>VCS.Entities.Invoice</c>
	///</summary>
	public enum InvoiceChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Company</c> at CompanyCodeSource
		///</summary>
		[ChildEntityType(typeof(Company))]
		Company,
			
		///<summary>
		/// Composite Property for <c>Patient</c> at PatientCodeSource
		///</summary>
		[ChildEntityType(typeof(Patient))]
		Patient,
			
		///<summary>
		/// Composite Property for <c>PatientActivation</c> at TransactionIdSource
		///</summary>
		[ChildEntityType(typeof(PatientActivation))]
		PatientActivation,
			
		///<summary>
		/// Composite Property for <c>PaymentMode</c> at PaymentModeSource
		///</summary>
		[ChildEntityType(typeof(PaymentMode))]
		PaymentMode,
	
		///<summary>
		/// Collection of <c>Invoice</c> as OneToMany for InvoiceDetailsCollection
		///</summary>
		[ChildEntityType(typeof(TList<InvoiceDetails>))]
		InvoiceDetailsCollection,

		///<summary>
		/// Collection of <c>Invoice</c> as OneToMany for PrescriptionDetailCollection
		///</summary>
		[ChildEntityType(typeof(TList<PrescriptionDetail>))]
		PrescriptionDetailCollection,
	}
	
	#endregion InvoiceChildEntityTypes
	
	#region InvoiceFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;InvoiceColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Invoice"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class InvoiceFilterBuilder : SqlFilterBuilder<InvoiceColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the InvoiceFilterBuilder class.
		/// </summary>
		public InvoiceFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the InvoiceFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public InvoiceFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the InvoiceFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public InvoiceFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion InvoiceFilterBuilder
	
	#region InvoiceParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;InvoiceColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Invoice"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class InvoiceParameterBuilder : ParameterizedSqlFilterBuilder<InvoiceColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the InvoiceParameterBuilder class.
		/// </summary>
		public InvoiceParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the InvoiceParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public InvoiceParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the InvoiceParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public InvoiceParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion InvoiceParameterBuilder
	
	#region InvoiceSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;InvoiceColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Invoice"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class InvoiceSortBuilder : SqlSortBuilder<InvoiceColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the InvoiceSqlSortBuilder class.
		/// </summary>
		public InvoiceSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion InvoiceSortBuilder
	
} // end namespace
